const js_md5 = require('js-md5');

const randomLetter = (length = 6, type = 'mixed') => {
  const lowercaseLetters = "abcdefghijklmnopqrstuvwxyz".split('');
  const uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
  const allLetters = lowercaseLetters.concat(uppercaseLetters);
  let result = '';

  switch (type) {
    case 'uppercase':
      for (let i = 0; i < length; i++) {
        const randomIndex = Math.floor(Math.random() * uppercaseLetters.length);
        result += uppercaseLetters[randomIndex];
      }
      break;
    case 'mixed':
      for (let i = 0; i < length; i++) {
        const randomIndex = Math.floor(Math.random() * allLetters.length);
        result += allLetters[randomIndex];
      }
      break;
    default:
      for (let i = 0; i < length; i++) {
        const randomIndex = Math.floor(Math.random() * lowercaseLetters.length);
        result += lowercaseLetters[randomIndex];
      }
      break;
  }

  return result;
};

once = randomLetter(5) + Math.floor(Math.random() * 0x989680) + randomLetter(5)

console.log(once)




const getSign = (params, secret) => {
  params.nonce = once
  // 定义一些常量和辅助函数
  const getStringFromHex = (a, b, c, d, e) => {
    return String.fromCharCode(d + 0x103, e);
  };

  // 初始化变量
  let signatureString = '';
  const sortedKeys = Object.keys(params).sort();

  // 遍历排序后的键
  sortedKeys.forEach(key => {
    const value = params[key];
    if (value !== undefined && key !== 'sign' && typeof value !== 'object') {
      signatureString += `${key}=${value}&`;
    }
  });

  // 去掉最后一个多余的 &
  if (signatureString.endsWith('&')) {
    signatureString = signatureString.slice(0, -1);
  }


  signatureString += `&key=&&$#fwefw%43@422`;
  console.log(signatureString)
  // 生成 MD5 哈希
  const md5Hash = js_md5(signatureString);
  return {
    once:once,
    md5:md5Hash
  };
};

//// 使用示例
//const params = {
//    pageNum: 1,
//    pageSize: 10,
//    type: 1,
//    key:'',
//    screen: 1,
//    sortType: 1,
//    getType:'',
//    categoryId:'',
//    'sign_appid': 'h5_23514861',
//    timestamp: 1740707569,
//    nonce: once
//}
//const secret = '看你麻痹看'; // 假设有一个密钥
//console.log(getSign(params, secret));
////
////categoryId=&getType=&key=&nonce=RWMqW1415814WeHiR&pageNum=1&pageSize=10&screen=1&sign_appid=h5_23514861&sortType=1&type=1&key=&&$#fwefw%43@422
//categoryId=&getType=&key=&nonce=RWMqW1415814WeHiR&pageNum=1&pageSize=10&screen=1&sign_appid=h5_23514861&sortType=1&timestamp=1740706119&type=1&key=&&$#fwefw%43@422
//categoryId=&getType=&key=&nonce=ypMXa9677886nUtuT&pageNum=1&pageSize=10&screen=1&sign_appid=h5_23514861&sortType=1&timestamp=1740707108&type=1&key=&&$#fwefw%43@422
